package com.nondo.dean.dealer.controller.api.app;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.nondo.dean.common.annotation.ApiVersion;
import com.nondo.dean.common.tool.BeanUtils;
import com.nondo.dean.common.tool.ValidateUtil;
import com.nondo.dean.common.web.Page;
import com.nondo.dean.common.web.Query;
import com.nondo.dean.common.web.View;
import com.nondo.dean.dealer.domain.StoreProduct;
import com.nondo.dean.dealer.model.StoreProductBean;
import com.nondo.dean.dealer.model.param.StoreProductParam;
import com.nondo.dean.dealer.model.result.StoreProductResult;
import com.nondo.dean.dealer.service.StoreProductService;
import com.nondo.dean.dealer.service.UserService;

/**
 * 店铺产品管理APP接口
 * @author zlj
 * @date 2018年5月2日
 */
@RestController("apiAppStoreProduct")
@RequestMapping("/{v}/app/store/product")
public class StoreProductController {
    
    @Autowired
    StoreProductService storeProductService;
    
    @Autowired
    UserService userService;
    
    /**
     * 更新店铺产品
     * @param request
     * @param response
     * @param storeProduct 店铺产品信息 {@link StoreProduct}
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/update")
    public View<StoreProduct> update(HttpServletRequest request,
            HttpServletResponse response,
            @Validated @RequestBody StoreProduct storeProduct, BindingResult result) {
        View<StoreProduct> view = new View<StoreProduct>(20000, "更新成功");

        if (result.hasErrors()) {
            view.setCode(40000);
            view.setMessage(ValidateUtil.first(result.getAllErrors()));
        } else {
            StoreProduct storeProductDb = this.storeProductService.select(storeProduct.getId());
            BeanUtils.copy(storeProduct, storeProductDb);
            this.storeProductService.update(storeProductDb);
            view.setData(storeProductDb);
        }

        return view;
    }
    
    /**
     * 上下架店铺产品
     * @param request
     * @param response
     * @param storeProduct 店铺产品信息 {@link StoreProduct}
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/shelf")
    public View<StoreProduct> shelf(HttpServletRequest request,
            HttpServletResponse response,
            @Validated @RequestBody StoreProductParam productParam, BindingResult result) {
        View<StoreProduct> view = new View<StoreProduct>(20000, "更新成功");

        if (result.hasErrors()) {
            view.setCode(40000);
            view.setMessage(ValidateUtil.first(result.getAllErrors()));
        } else {
            for (String id : productParam.getIds().split(",")) {
                StoreProduct storeProductDb = this.storeProductService.select(Integer.parseInt(id));
                if (storeProductDb != null) {
                    storeProductDb.setShelfStatus(productParam.getShelfStatus());
                    this.storeProductService.update(storeProductDb); 
                }
            }
        }

        return view;
    }

    /**
     * 获取店铺产品分页查询列表
     * @param request
     * @param response
     * @param query
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/list")
    public View<Page<StoreProductResult>> list(HttpServletRequest request, HttpServletResponse response, @RequestBody Query<StoreProductBean> query) {
        View<Page<StoreProductResult>> view = new View<Page<StoreProductResult>>(20000, "获取列表成功");

        StoreProductBean storeProduct = query.getData();
        
        if (storeProduct == null) {
            storeProduct = new StoreProductBean();
        }
        Integer storeId = userService.queryStoreId();
        // 只能查询自己店铺的产品
        storeProduct.setStoreId(storeId);
        storeProduct.setProductCode(storeProduct.getProductName());

        Page<StoreProductResult> page = new Page<StoreProductResult>(query.getPage(), query.getSize(), storeProductService.count(storeProduct));
        
        List<StoreProduct> storeProductList = storeProductService.getList(query.getOffset(), query.getLimit(), storeProduct);
        List<StoreProductResult> storeProductResultList = storeProductService.getStoreProductResultList(storeProductList);
        page.setContent(storeProductResultList);
        
        view.setData(page);
        
        return view;
    }

}